package com.haoxi.gateway.test.message;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.haoxi.gateway.common.TestConfConst;
import com.haoxi.gateway.test.SGIPConstant;
import com.haoxi.gateway.test.util.TypeConvert;

public class SGIPDeliverMessage extends SGIPMessage {
    private static SimpleDateFormat format = new SimpleDateFormat("MMddHHmmss");

    public SGIPDeliverMessage(String UserNumber, String SPNumber, int tP_pid,
            int tP_udhi, int msg_Fmt, byte[] msg_Content, String reserve)
            throws IllegalArgumentException {
        // 这里就要生成seqID拉
        // SgipEncoder encoder = new SgipEncoder(20);
        // UnicomHeader header = new UnicomHeader();
        int msgContentLen = msg_Content.length;
        int allmsgLength = 87 + msgContentLen;
        buf = new byte[allmsgLength];
        TypeConvert.int2byte(allmsgLength, buf, 0);
        TypeConvert.int2byte(SGIPConstant.Deliver_Command_Id, buf, 4);
        int icpid = TestConfConst.icpid & 0xffffffff;
        TypeConvert.int2byte(icpid, buf, 8);
        int timeStamp = Integer.parseInt(format.format(new Date()));
        TypeConvert.int2byte(timeStamp, buf, 12);
        setSequenceID();
        System
                .arraycopy(UserNumber.getBytes(), 0, buf, 20, UserNumber
                        .length());
        System.arraycopy(SPNumber.getBytes(), 0, buf, 41, SPNumber.length());
        buf[62] = (byte) tP_pid;
        buf[63] = (byte) tP_udhi;
        buf[64] = (byte) msg_Fmt;
        byte[] messageLength = TypeConvert.int2byte(msgContentLen);
        System.arraycopy(messageLength, 0, buf, 65, 4);
        System.arraycopy(msg_Content, 0, buf, 69, msgContentLen);
        System.arraycopy(reserve.getBytes(), 0, buf, 69 + msgContentLen, 8);
    }

    public String getUserNumber() {
        byte tmpId[] = new byte[21];
        System.arraycopy(buf, 20, tmpId, 0, 21);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public String getSPNumber() {
        byte tmpId[] = new byte[21];
        System.arraycopy(buf, 41, tmpId, 0, 21);
        String tmpStr = (new String(tmpId)).trim();
        if (tmpStr.indexOf(0) >= 0)
            return tmpStr.substring(0, tmpStr.indexOf(0));
        else
            return tmpStr;
    }

    public int getTpPid() {
        int tmpId = buf[62];
        return tmpId;
    }

    public int getTpUdhi() {
        int tmpId = buf[63];
        return tmpId;
    }

    public int getMsgFmt() {
        int tmpFmt = buf[64];
        return tmpFmt;
    }

    public int getMsgLength() {
        return TypeConvert.byte2int(buf, 65);
    }

    public byte[] getMsgContent() {
        int len = getMsgLength();
        byte tmpContent[] = new byte[len];
        System.arraycopy(buf, 69, tmpContent, 0, len);
        return tmpContent;
    }

    public String getReserve() {
        int loc = 69 + getMsgLength();
        byte tmpReserve[] = new byte[8];
        System.arraycopy(buf, loc, tmpReserve, 0, 8);
        return (new String(tmpReserve)).trim();
    }

    public String toString() {
        String tmpStr = "SGIP_DELIVER: ";
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nmsgLength=")
                .append(this.getallLength())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nCommandId=")
                .append(getCommandId())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nSrcNodeId=")
                .append(getSrcNodeId())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nTime_Stamp=")
                .append(getTime_Stamp())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nSequence_Id=")
                .append(getSequenceId())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nUserNumber=")
                .append(getUserNumber())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nSPNumber=")
                .append(getSPNumber())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nTpPid=").append(
                getTpPid())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nTpUdhi=")
                .append(getTpUdhi())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nMsgFmt=")
                .append(getMsgFmt())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nMsgLength=")
                .append(getMsgLength())));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nMsgContent=")
                .append(new String(getMsgContent()))));
        tmpStr = String.valueOf(String.valueOf((new StringBuffer(String
                .valueOf(String.valueOf(tmpStr)))).append("\r\nReserve=")
                .append(getReserve())));
        return tmpStr;
    }

    public int getCommandId() {
        return TypeConvert.byte2int(buf, 4);
    }

    public int getSrcNodeId() {
        return TypeConvert.byte2int(buf, 8);
    }

    public int getTime_Stamp() {
        return TypeConvert.byte2int(buf, 12);
    }

    public int getSequenceId() {
        return TypeConvert.byte2int(buf, 16);
    }

    public String getSeqStr() {
        return "" + getSrcNodeId() + getTime_Stamp() + getSequenceId();
    }

    public int getallLength() {
        return TypeConvert.byte2int(buf, 0);
    }

}
